/**
 * Input is a subclass of the class Put.
 * An put has one field, which is a value.
 * 
 * @author Gregory Banfi (gregory.banfi@usi.ch), @author Simone d'Avico (simone.davico@usi.ch); Giuseppe Mendola (giuseppe.mendola@usi.ch);
 * @version 17.05.2011
 */

package ch.usi.inf.pf2.puts;

import java.awt.Color;
import java.awt.Graphics;
import java.io.Serializable;

import ch.usi.inf.pf2.Value;
import ch.usi.inf.pf2.pins.OutputPin;

public class Input extends Put implements Serializable {

	// Fields for the GUI
	private int left;
	private int top;
	private int width;
	private int height;
	private OutputPin outputPin;
	private boolean multipleInput;
	private String inputString;


	/**
	 * Constructor of Input. It assigns a value to the value field of the put
	 * object.
	 * 
	 * @param value
	 *            A value object.
	 */
	public Input(final Value value, final int x, final int y) {
		super(value, x, y);
		this.multipleInput=false;
		outputPin=new OutputPin(value);
		this.left = x;
		this.top = y;
		width = 20;
		height = 20;
	}

	
	public boolean isMultipleInput() {
		return multipleInput;
	}


	public void setMultipleInput(boolean multipleInput) {
		this.multipleInput = multipleInput;
	}
	
	/**
	 * Methods for the GUI
	 */
	/**
	 * This method draws an input with the defined dimension.
	 */
	@Override
	public void draw(Graphics g, Color color) {
		if (multipleInput==true){
			g.drawRect(left, top, width, height);
			g.drawString(inputString, left+7, top+15);
			g.drawLine(left + width, top + height / 2, left + width + 3, top
					+ height / 2);
		}
		else{
		if (getValue() == Value.TRUE) {
			g.setColor(Color.GREEN);
			inputString="1";
		} else if (getValue() == Value.FALSE) {
			g.setColor(Color.RED);
			inputString="0";
		} else if (getValue() == Value.UNKNOWN) {
			g.setColor(color);
			inputString="X";
		}
		g.drawRect(left, top, width, height);
		g.drawString(inputString, left+7, top+15);
		g.drawLine(left + width, top + height / 2, left + width + 3, top
				+ height / 2);
		}
	}
	

	/**
	 * Override methods form component.
	 */
	@Override
	// This method checks if there is an input where the user clicks the mouse.
	public boolean contains(int x, int y) {
		return x >= left && x <= left + width && y >= top && y <= top + height;
	}

	@Override
	// This method returns the left field.
	public int getOriginX() {
		return left;
	}

	@Override
	// This method returns the top field.
	public int getOriginY() {
		// TODO Auto-generated method stub
		return top;
	}
	
	@Override
	public void setOutputPin(OutputPin outputPin){
		this.outputPin=outputPin;
	}
	
	@Override
	public void setOrigin(int x, int y){
		this.left=x;
		this.top=y;
	}
	@Override
	public OutputPin getOutputPin(){
		return outputPin;
	}


	public void setInputString(String inputString) {
		this.inputString = inputString;
	}


	public void setWidth(int width) {
		this.width = width;
	}


	public int getWidth() {
		return width;
	}
}
